package cn.iocoder.yudao.module.crossborder.order.service.ordercustomeraddress;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordercustomeraddress.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordercustomeraddress.OrderCustomerAddressDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.order.dal.mysql.ordercustomeraddress.OrderCustomerAddressMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.order.enums.ErrorCodeConstants.*;

/**
 * 跨境电商客户地址 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderCustomerAddressServiceImpl implements OrderCustomerAddressService {

    @Resource
    private OrderCustomerAddressMapper orderCustomerAddressMapper;

    @Override
    public Long createOrderCustomerAddress(OrderCustomerAddressSaveReqVO createReqVO) {
        // 插入
        OrderCustomerAddressDO orderCustomerAddress = BeanUtils.toBean(createReqVO, OrderCustomerAddressDO.class);
        orderCustomerAddressMapper.insert(orderCustomerAddress);

        // 返回
        return orderCustomerAddress.getId();
    }

    @Override
    public void updateOrderCustomerAddress(OrderCustomerAddressSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderCustomerAddressExists(updateReqVO.getId());
        // 更新
        OrderCustomerAddressDO updateObj = BeanUtils.toBean(updateReqVO, OrderCustomerAddressDO.class);
        orderCustomerAddressMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderCustomerAddress(Long id) {
        // 校验存在
        validateOrderCustomerAddressExists(id);
        // 删除
        orderCustomerAddressMapper.deleteById(id);
    }

    @Override
        public void deleteOrderCustomerAddressListByIds(List<Long> ids) {
        // 删除
        orderCustomerAddressMapper.deleteByIds(ids);
        }


    private void validateOrderCustomerAddressExists(Long id) {
        if (orderCustomerAddressMapper.selectById(id) == null) {
            throw exception(ORDER_CUSTOMER_ADDRESS_NOT_EXISTS);
        }
    }

    @Override
    public OrderCustomerAddressDO getOrderCustomerAddress(Long id) {
        return orderCustomerAddressMapper.selectById(id);
    }

    @Override
    public PageResult<OrderCustomerAddressDO> getOrderCustomerAddressPage(OrderCustomerAddressPageReqVO pageReqVO) {
        return orderCustomerAddressMapper.selectPage(pageReqVO);
    }

}